package org.liquidByte.simpleChat.guiClient;
import java.util.ArrayList;
import java.util.List;

import org.liquidByte.simpleChat.dto.User;
import org.liquidByte.simpleChat.dto.UserIdentification;
import org.liquidByte.simpleChat.dto.UserMessage;

public class EventService {
	
	private static List<UserOnlineEventListener> usrOnlineEvent;
	private static List<UserOfflineEventListener> usrOfflineEvent;
	private static List<StatusChangedEventListener> usrStatusChangedEvent;
	private static List<MessageReceivedEventListener> messageReceivedEvent;
	
	static {
		EventService.usrOnlineEvent = new ArrayList<UserOnlineEventListener>();
		EventService.usrOfflineEvent = new ArrayList<UserOfflineEventListener>();
		EventService.usrStatusChangedEvent = new ArrayList<StatusChangedEventListener>();
		EventService.messageReceivedEvent = new ArrayList<MessageReceivedEventListener>();
	}
	
	//-----------------fireEvents--------------------------//
	
	private void fireUserOnline(User user){
		for(UserOnlineEventListener listener : EventService.usrOnlineEvent){
			listener.onUserOnline(user);
		}
	}
	
	private void fireUserOffline(UserIdentification userID){
		for(UserOfflineEventListener listener : EventService.usrOfflineEvent){
			listener.onUserOffline(userID);
		}
	}
	
	private void fireStatusChanged(User user){
		for(StatusChangedEventListener listener : EventService.usrStatusChangedEvent){
			listener.onStatusChanged(user);
		}
	}
	
	private void fireMessageReceived(UserMessage userMsg){
		for(MessageReceivedEventListener listener : EventService.messageReceivedEvent){
			listener.onMessageReceived(userMsg);
		}
	}
	
	//-----------------End - fireEvents--------------------------//
	
	//----------------------add's and removes's Listeners----------------------------------//
	public static void addOnlineListener(UserOnlineEventListener listener){
		EventService.usrOnlineEvent.add(listener);
	}
	
	public static void removeOnlineListener(UserOnlineEventListener listener){
		EventService.usrOnlineEvent.remove(listener);
	}
	
	public static void addOfflineListener(UserOfflineEventListener listener){
		EventService.usrOfflineEvent.add(listener);
	}
	
	public static void removeOfflineListener(UserOfflineEventListener listener){
		EventService.usrOfflineEvent.remove(listener);
		
	}
	
	public static void addMessageListener(MessageReceivedEventListener listener){
		EventService.messageReceivedEvent.add(listener);
	}
	
	public static void removeMessageListener(MessageReceivedEventListener listener){
		EventService.messageReceivedEvent.remove(listener);
	}
	
	public static void addStatusListener (StatusChangedEventListener listener){
		EventService.usrStatusChangedEvent.add(listener);
	}
	
	public static void removeStatusListener (StatusChangedEventListener listener){
		EventService.usrStatusChangedEvent.remove(listener);
	}
	
	//----------------------End - add's and removes's Listeners----------------------------------//
	
	public void onUserOnline(User user) {
		fireUserOnline(user);
	}
	
	public void onUserOffline(UserIdentification userIdent) {
		fireUserOffline(userIdent);
	}
	
	
	public void onStatusChanged(User user) {
		fireStatusChanged(user);
	}
	
	public void onMessageReceived(UserMessage userMsg){
		fireMessageReceived(userMsg);
	}
	
}
